﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebShop.Classes.DataSource;
using WebShop.Classes;
using WebShop.Classes.Messaging;

namespace WebShop.Controllers
{
    public abstract class BaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            ConfigStorage.Language = (Request.Cookies.AllKeys.Contains(ParameterConstants.Language)) ? (Request.Cookies[ParameterConstants.Language].Value) : "eng";

            if (Request.QueryString[ParameterConstants.Language] != null)
            {
                string language = Request.QueryString[ParameterConstants.Language];

                IDataSource dataSource = DataSourceFactory.Contruct();
                Dictionary<string, string> languages = dataSource.GetLanguages();

                if (languages.ContainsKey(language)) {
                    ConfigStorage.Language = language;
                    HttpCookie cookie = new HttpCookie(ParameterConstants.Language);
                    cookie.Value = language;
                    cookie.Expires = DateTime.Now.AddYears(100); //i think, 100 years is  normal value
                    HttpContext.Response.Cookies.Add(cookie);
                }
            }

            if (ConfigStorage.Order == null)
            {
                ConfigStorage.Order = new DataService.OrderFull();
                ConfigStorage.Order.Composition = new List<DataService.OrderPosition>();
            }

            base.OnActionExecuting(filterContext);
        }

        public ActionResult Error404 ()
        {
            return View("Error404");
        }
    }
}
